﻿using Bing.Maps;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Windows.Foundation;
using Windows.UI;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;

namespace MovingChame.WindowsStoreApp.Helpers
{
    public static class MapHelper
    {
        public static Point GetInfoboxOffset(Location location, Map map, int offsetFactor)
        {
            Point pinPoint;
            map.TryLocationToPixel(location, out pinPoint);
            return GetInfoboxOffset(pinPoint, map, offsetFactor);
        }

        public static Point GetInfoboxOffset(Point anchor, Map map, int offsetFactor)
        {
            Point offset = new Point(0, 0);
            int quadKey = 0;

            if (anchor.X > map.Width / 2)
                quadKey++;

            if (anchor.Y > map.Height / 2)
                quadKey += 2;

            switch (quadKey)
            {
                case 0:
                    offset = new Point(offsetFactor, 0);
                    break;
                case 1:
                    offset = new Point(-1 * offsetFactor, 0);
                    break;
                case 2:
                    offset = new Point(offsetFactor, -1 * offsetFactor);
                    break;
                case 3:
                    offset = new Point(-1 * offsetFactor, -1 * offsetFactor);
                    break;
            }

            return offset;
        }

        public static Button CreateLinkButton(string text, string tag, Style style, RoutedEventHandler function)
        {
            Button button = new Button();
            button.Content = text;
            button.Tag = tag;
            button.Click += function;
            button.Style = style;

            return button;
        }

        public static MapType MapTypeFromString(string mapType)
        {
            if (mapType.EndsWith("Aerial"))
                return MapType.Aerial;
            else if (mapType.EndsWith("Birdseye"))
                return MapType.Birdseye;
            else if (mapType.EndsWith("Empty"))
                return MapType.Empty;
            else
                return MapType.Road;
        }

        public static TimeSpan MapAnimationDurationFromString(string mapAnimationDuration)
        {
            if (mapAnimationDuration.EndsWith("Default"))
                return MapAnimationDuration.Default;
            else
                return MapAnimationDuration.None;
        }

        public static MapViewRestriction MapViewRestrictionFromString(string mapViewRestriction)
        {
            if (mapViewRestriction.EndsWith("MapOnly"))
                return MapViewRestriction.MapOnly;
            else if (mapViewRestriction.EndsWith("None"))
                return MapViewRestriction.None;
            else if (mapViewRestriction.EndsWith("OneWorldOnly"))
                return MapViewRestriction.OneWorldOnly;
            else
                return MapViewRestriction.ZoomOutToWholeWorld;
        }

        public static PreloadArea PreloadAreaFromString(string preloadArea)
        {
            if (preloadArea.EndsWith("None"))
                return PreloadArea.None;
            else if (preloadArea.EndsWith("Small"))
                return PreloadArea.Small;
            else if (preloadArea.EndsWith("Medium"))
                return PreloadArea.Medium;
            else
                return PreloadArea.Large;
        }

        public static MapForegroundPosition MapForegroundPositionFromString(string mapForegroundPosition)
        {
            if (mapForegroundPosition.EndsWith("BottomLeft"))
                return MapForegroundPosition.BottomLeft;
            else if (mapForegroundPosition.EndsWith("BottomRight"))
                return MapForegroundPosition.BottomRight;
            else if (mapForegroundPosition.EndsWith("TopLeft"))
                return MapForegroundPosition.TopLeft;
            else
                return MapForegroundPosition.TopRight;
        }

        public static Color ColorFromString(string s)
        {
            if (s.EndsWith("Red"))
                return Colors.Red;
            else if (s.EndsWith("White"))
                return Colors.White;
            else if (s.EndsWith("Black"))
                return Colors.Black;
            else if (s.EndsWith("Blue"))
                return Colors.Blue;
            else
                return Colors.Green;
        }
    }
}